package com.example.ui.contact.adapter;

import java.util.List;
import java.util.Map;

import com.example.huashangapp.R;
import com.example.util.BitmapLoad;
import com.example.util.Util;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryAdapter extends BaseAdapter {
	
	private Context mContext;
	private List<Map<String, Object>> imgList;
	//private int[] galleryArr = {R.drawable.wall_bg};

	public GalleryAdapter(Context context,List<Map<String, Object>> imgWallList){
		mContext = context;
		imgList = imgWallList;
	}

	@Override
	public int getCount() {
//		if(imgList.size()==0){
//			//return galleryArr[0];
//			HomePageView.imgWall.setVisibility(View.GONE);
//			HomePageView.imgWall0Img.setVisibility(View.VISIBLE);
//			return 0;
//		}
//		HomePageView.imgWall0Img.setVisibility(View.GONE);
//		HomePageView.imgWall.setVisibility(View.VISIBLE);
		return imgList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
//	public void addItem(Map<String, Bitmap> b){
//		imgList.add(b);
//	}

	@SuppressWarnings("deprecation")
	@SuppressLint("InlinedApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView=null;
		Holder holder = null;
		if(convertView == null){
			holder = new Holder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.homepage_gallery_img, null);
			holder.img = (ImageView)convertView.findViewById(R.id.homepage_gallery_img);
			convertView.setTag(holder);
		}else{
			holder=(Holder)convertView.getTag();
			
		}
		
		
		try {
			Bitmap bit = null;
			
//			String[] imgSZ = null;
//			String imgUrl = (String)imgList.get(position).get("photo");
//			imgSZ = imgUrl.split("");
//			if(imgSZ!=null){
//				for(int i=0;i<imgSZ.length;i++){
//					bit=BitmapLoad.loadBitmap(imgSZ[i]);
//					Drawable dra = new BitmapDrawable(bit);
//					holder.img.setBackgroundDrawable(dra);
//				}
//			}else{
//				holder.img.setBackgroundResource(R.drawable.wall_bg);
//			}
			
			String imgUrl = (String)imgList.get(position).get("photo").toString();
			if(imgUrl!=null){
				bit=BitmapLoad.loadBitmap(mContext,imgUrl);
				Drawable dra = new BitmapDrawable(bit);
				holder.img.setBackgroundDrawable(dra);
			}else{
				Util.toast(mContext, "网络连接有误，请重试");
				holder.img.setBackgroundResource(R.drawable.wall_bg);
			}
			
			holder.img.setScaleType(ImageView.ScaleType.CENTER);
			holder.img.setAdjustViewBounds(true);
			holder.img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//			holder.img.setOnClickListener(new OnClickListener() {
//				
//				@Override
//				public void onClick(View view) {
//					// TODO Auto-generated method stub
//					Message msg = HomePageView.handler.obtainMessage(HomePageView.SET_WALL_GALLERY);
//					msg.what = HomePageView.SET_WALL_GALLERY;
//					msg.sendToTarget();
//				}
//			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return convertView;  
	}

	
	class Holder {
		ImageView img;
	}
	
	

}
